package com.panda.pandawash.model;

/**
 * Created by gaowf on 2017/2/22.
 */

public class MotorSingleStatusMessage extends MotorStatusMessage {

    public final static String MOTOR_SINGLE_STATUS_CURRENT = "motor_single_current";
    public final static String MOTOR_SINGLE_STATUS_VOLTAGE = "motor_single_voltage";
    public final static String MOTOR_LEAK_STATUS_CURRENT = "motor_leak_current";
    public final static String MOTOR_JIFEN_STATUS_CURRENT = "motor_jifen_current";
    public final static String MOTOR_POWERON_STATUS_CURRENT = "motor_poweron_status";
    public final static String MOTOR_RED_LED_STATUS_CURRENT = "motor_red_led_status";
    public final static String MOTOR_YELLOW_LED_STATUS_CURRENT = "motor_yellow_led_status";
    public final static String MOTOR_RUNING_LED_STATUS_CURRENT = "motor_running_led_status";
    public final static String MOTOR_SIGNAL_LED_STATUS_CURRENT = "motor_signal_led_status";

    public final static String MOTOR_IS_WORKING = "motor_is_working";

    private float motorStatusWorkingcurrent;
    private float motorStatusWorkingvoltage;
    private int motorStatusIntegralValue;
    private int motorStatusIsWorking;
    private int motorStatusRunnedSecond;
    private int motorStatusUsedJoule;
    private int motorStatusWaterTime;

    private int tan = 0;
    private float Lref = (float) 0.00;

    private int idle_load = 812;
    private int full_load = 498;

    // 解析二
    public void handle_motor_message(byte[] message, int tanvalue, float lrefvalue, int e_load, int f_load) {

        //Log.i("bear_", "message2: " + message.length);
        int max_size = message.length;

        tan = tanvalue;
        Lref = lrefvalue;

        if (e_load > 0)
            idle_load = e_load;
        if (f_load > 0)
            full_load = f_load;
        if (max_size < 16) {
            return;
        }

        boolean t_motor_red_led = motor_red_led;
        boolean t_motor_yellow_led = motor_yellow_led;
        boolean t_motor_runing_led = motor_runing_led;
        boolean t_motor_signal_led = motor_signal_led;
        float t_motor_leakcurrent = motor_leakcurrent;
        float t_motorStatusWorkingcurrent = motorStatusWorkingcurrent;
        float t_motorStatusWorkingvoltage = motorStatusWorkingvoltage;
        int t_motorStatusIntegralValue = motorStatusIntegralValue;
        int t_motorStatusIsWorking = motorStatusIsWorking;
        int t_motorStatusRunnedSecond = motorStatusRunnedSecond;
        int t_motorStatusUsedJoule = motorStatusUsedJoule;
        int t_motorStatusWaterTime = motorStatusWaterTime;

        if (max_size >= 16) {

            if (((message[9] & 0xff) & 0x08) > 0) {
                t_motor_red_led = true;
            } else {
                t_motor_red_led = false;
            }

            if (((message[9] & 0xff) & 0x10) > 0) {
                t_motor_yellow_led = true;
            } else {
                t_motor_yellow_led = false;
            }

            if (((message[9] & 0xff) & 0x20) > 0) {
                t_motor_runing_led = true;
            } else {
                t_motor_runing_led = false;
            }

            if (((message[9] & 0xff) & 0x40) > 0) {
                t_motor_signal_led = false;
            } else {
                t_motor_signal_led = true;
            }

            float para = (float) (0.33 * tan / 1024);
            t_motor_leakcurrent = (float) (((message[5] & 0xff) + (message[6] & 0xff) * 256) * 30 / Lref);
            t_motorStatusWorkingcurrent = (float) (((message[3] & 0xff) + (message[4] & 0xff) * 256) * para);

            if (message[14] == 1 && t_motor_runing_led) {
                t_motorStatusWorkingvoltage = (float) ((float) (((message[7] & 0xff) + (message[8] & 0xff) * 256)) * 200.0 / full_load);
            } else {
                t_motorStatusWorkingvoltage = (float) ((float) (((message[7] & 0xff) + (message[8] & 0xff) * 256)) * 200.0 / idle_load);
            }
            t_motorStatusIntegralValue = ((int) message[11] & 0xff) << 24 + ((int) message[12] & 0xff) << 16 + ((int) message[13] & 0xff) << 8 + ((int) message[14] & 0xff);

            t_motorStatusIsWorking = message[14] & 0xff;

            if (t_motorStatusIsWorking == 0)
                t_motor_signal_led = false;
        }

        if (max_size > 16) {
            t_motorStatusRunnedSecond = (message[15] & 0xff) * 60 + (message[16] & 0xff);
        }
        if (max_size > 18) {
            t_motorStatusUsedJoule = (message[17] & 0xff) + (message[18] & 0xff) * 256;
        }
        if (max_size > 20) {
            t_motorStatusWaterTime = (message[19] & 0xff) * 60 + (message[20] & 0xff);
        }
        if (t_motorStatusWorkingvoltage > 270 || (t_motorStatusWorkingvoltage > 100 && t_motorStatusWorkingvoltage < 170) || t_motorStatusWorkingvoltage < 10) {
            //return;
            t_motorStatusWorkingvoltage = motorStatusWorkingvoltage;
        }
        motor_red_led = t_motor_red_led;
        motor_yellow_led = t_motor_yellow_led;
        motor_runing_led = t_motor_runing_led;
        motor_signal_led = t_motor_signal_led;
        motor_leakcurrent = t_motor_leakcurrent;
        motorStatusWorkingcurrent = t_motorStatusWorkingcurrent;
        motorStatusWorkingvoltage = t_motorStatusWorkingvoltage;
        motorStatusIntegralValue = t_motorStatusIntegralValue;
        motorStatusIsWorking = t_motorStatusIsWorking;
        motorStatusRunnedSecond = t_motorStatusRunnedSecond;
        motorStatusUsedJoule = t_motorStatusUsedJoule;
        motorStatusWaterTime = t_motorStatusWaterTime;

    }

    public boolean MotorRedLed() {
        return motor_red_led;
    }

    public boolean MotorYellowLed() {
        return motor_yellow_led;
    }

    public boolean MotorRuningLed() {
        return motor_runing_led;
    }

    public void set_motor_runing_led(boolean flag)
    {
        motor_runing_led = flag;
    }

    public boolean MotorSignalLed() {
        return motor_signal_led;
    }

    public int MotorStatusRunnedSecond() {
        return motorStatusRunnedSecond;
    }

    public int MotorStatusUsedJoule() {
        return motorStatusUsedJoule;
    }

    public int MotorStatusUsedWaterTime() {
        return motorStatusWaterTime;
    }

    public String motorSingleStatusLeakCurrent() {

        return String.format("%.2f", motor_leakcurrent) + " 安";
    }

    public String motorSingleStatusCurrent() {

        return String.format("%.2f", motorStatusWorkingcurrent) + " 安";
    }

    public String motorSingleStatusVoltage() {

        return String.format("%.1f", motorStatusWorkingvoltage) + " 伏";
    }

    public boolean motorIsWorking() {

        if (motorStatusIsWorking == 1) {
            return true;
        } else {
            return false;
        }

    }


    public void setMotorWorkingStatus(boolean flag){
        if(flag){
            motorStatusIsWorking = 1;
        }else {
            motorStatusIsWorking = 0;
        }
    }

    public String motorSingleIsWorking() {
        String statussString = "";

        switch (motorStatusIsWorking) {
            case 0:
                statussString = "停止";
                break;

            case 1:
                statussString = "运行";
                break;

            default:
                statussString = "未知";
                break;
        }

        return statussString;

    }

    public boolean isMotorStatusMessage(byte[] msg) {
        //	Log.i("bear_", "~~~~~~~~~~~~~~~" + msg.length);
        if (msg.length >= 16) {
            if (msg[2] == 0x36) {
                return true;
            }

        }
        return false;
    }
}

